﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FlanProject.Combats.Actions
{
    public class RandomWalkCombatAction : CombatAction
    {
        #region State

        public override bool IsOffensive { get { return false; } }
        public override bool IsTargetNeeded { get { return false; } }

        #endregion

        #region Executing Data

        private static readonly int MOVE_LEFT = -1;
        private static readonly int MOVE_RIGHT = 1;
        private static readonly int backgroundOffset = 10;
        private Vector2 direction;
        private Vector2 destination;

        #endregion

        public RandomWalkCombatAction(CombatCharacter character)
            : base(character)
        {

        }

        #region Combat Stage

        protected override void StartStage()
        {
            switch (Stage)
            {
                case CombatActionStage.Preparing:
                {
                    var random = new Random();

                    // Move forward or backwards
                    direction.X = random.Next(0, 2) == 0 ? MOVE_LEFT : MOVE_RIGHT;
                    
                    // Randomize a new destination
                    destination.X = MathHelper.Clamp(Character.Position.X + random.Next(50, 100) * direction.X,
                        FlanProject.Instance.GraphicsDevice.Viewport.Bounds.Left + backgroundOffset,
                        FlanProject.Instance.GraphicsDevice.Viewport.Bounds.Right
                            - Character.CurrentAnimation.Animation.FrameList[Character.CurrentAnimation.FrameIndex].Width - backgroundOffset);

                    Character.FaceTarget(destination);
                    Character.CurrentState = CombatCharacter.States.Idle;

                    break;
                }

                case CombatActionStage.Executing:
                {
                    Character.CurrentState = CombatCharacter.States.Walking;
                    break;
                }

                case CombatActionStage.Finishing:
                {
                    Character.CurrentState = CombatCharacter.States.Idle;
                    break;
                }

                case CombatActionStage.Complete:
                {
                    Character.FaceTarget(Target.Position);
                    Character.CurrentState = CombatCharacter.States.Idle;
                    break;
                }
            }
        }

        protected override void UpdateCurrentStage(GameTime gameTime)
        {
            switch(Stage)
            {
                case CombatActionStage.Executing:
                {
                    // Move character to target destination
                    if (Character.CurrentState != CombatCharacter.States.Hit)
                    {
                        Character.PreviousPosition = Character.Position;
                        Character.Position += direction * Character.Stats.MovementSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }

                    break;
                }

                case CombatActionStage.Complete:
                {
                    // Set new original position
                    Character.OriginalPosition = Character.Position;
                    break;
                }
            }
        }

        protected override bool IsReadyForNextStage
        {
            get
            {
                switch (Stage)
                {
                    case CombatActionStage.Preparing:
                        return true;

                    case CombatActionStage.Advancing:
                        return true;

                    // Character either reached the destination or is stuck
                    case CombatActionStage.Executing:
                        return direction.X == MOVE_LEFT ? Character.Position.X < destination.X : Character.Position.X > destination.X;

                    case CombatActionStage.Returning:
                        return true;

                    case CombatActionStage.Finishing:
                        return true;
                }

                return base.IsReadyForNextStage;
            }
        }

        #endregion
    }
}